#pragma once
#include "Singleton.h"

struct FactoryType { virtual ~FactoryType() {} };

template <typename Base>
struct FactoryBase : public FactoryType
{
	virtual Base* create() const = 0;
};

struct FactoryRepos;
#define sFactRepos Singleton<FactoryRepos>::getMe()
template <typename T>
struct Factory : FactoryBase<typename T::Base>
{
	typename T::Base* create() const { return new T; }
	static void regist() { sFactRepos._factorys[std::make_pair(T::type(), T::id())] = new Factory; }
};

struct FactoryRepos
{
	std::map<std::pair<size_t, size_t>, FactoryType*> _factorys;

	template <typename Base>
	Base* create(size_t id) const
	{
		auto it = _factorys.find(std::make_pair(Base::type(), id));
		if (it == _factorys.end())
			return NULL;

		auto pFact = dynamic_cast<FactoryBase<Base>*>(it->second);
		if (!pFact)
			return NULL;

		return pFact->create();
	}
};
